"""
内容：层 Layer
日期：2020年7月9日
作者：Howie
"""

from keras.models import Model
from keras.layers import Input, Dense, Activation, Dropout, Flatten, Reshape
from keras.layers import Cropping2D, Conv2D, SeparableConv2D
from keras.layers import MaxPooling2D, AveragePooling2D, GlobalAveragePooling2D, GlobalMaxPooling2D
from keras.layers import Concatenate, BatchNormalization, ReLU
from keras.utils import plot_model

# 核心网络层
input_img = Input(shape=(256, 256, 1))
dense = Dense(units=3, use_bias=True)(input_img)
activation = Activation('relu')(dense)
dropout = Dropout(rate=0.25)(activation)
flatten = Flatten()(dropout)
reshape = Reshape(target_shape=(256, 256, 3))(flatten)

# 卷积层与池化层
crop2d = Cropping2D(cropping=16)(input_img)
conv2d = Conv2D(
    filters=32,
    kernel_size=(
        3,
        3),
    padding='same',
    activation='relu')(crop2d)
sep_conv2d = SeparableConv2D(filters=64, kernel_size=(
    3, 3), padding='same', activation='relu')(conv2d)
max_pool = MaxPooling2D(pool_size=(2, 2))(sep_conv2d)
avg_pool = AveragePooling2D(pool_size=(2, 2))(max_pool)
global_max_pool = GlobalMaxPooling2D()(sep_conv2d)
global_avg_pool = GlobalAveragePooling2D()(sep_conv2d)
concat = Concatenate(axis=1)([global_avg_pool, global_max_pool])
reshape_2 = Reshape(target_shape=(4, 4, 8))(concat)
bn = BatchNormalization()(reshape_2)


model = Model(inputs=input_img, outputs=avg_pool)
cnn_model = Model(inputs=input_img, outputs=bn)
plot_model(model=model, to_file='./logs/Demo7_Model.pdf', show_shapes=True)
plot_model(
    model=cnn_model,
    to_file='./logs/Demo7_CNN_Model.pdf',
    show_shapes=True)
